package com.gitee.neuray.security.gateway.strategy;

import com.gitee.neuray.security.admin.authority.PermissionElementDto;
import com.gitee.neuray.security.common.exception.BaseException;
import com.netflix.zuul.context.RequestContext;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: zhhongyu
 * @description: 校验uri权限
 * @since: Create in 9:37 2020/3/27
 */
public class UriPermission extends AbstratePermissionStrategy {

    @Override
    public PermissionElementDto checkout(List<PermissionElementDto> permissionInfos, RequestContext ctx) {
        String requestUri = getRequestUri(ctx);
        if (StringUtils.isEmpty(requestUri)) {
            throw new BaseException("请求的requesturi error... ");
        }
        checkargs(permissionInfos);
        List<PermissionElementDto> menus = permissionInfos.parallelStream().filter(entity -> requestUri.contains(entity.getUri())).collect(Collectors.toList());
        if (menus.size() == 0) {
            sendZuulResponseError40301();
            return null;
        }
        PermissionElementDto max = Collections.max(menus);
        return max;
    }
}
